package com.liyiqing.secondhandmall.memberservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.liyiqing.commonservice.utils.*;
import com.liyiqing.commonservice.vo.MemberCollectSpuVo;
import com.liyiqing.commonservice.vo.ProductShowVo;
import com.liyiqing.secondhandmall.memberservice.feign.ProductFeignService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.liyiqing.secondhandmall.memberservice.dao.MemberUserCollectSpuDao;
import com.liyiqing.secondhandmall.memberservice.entity.MemberUserCollectSpuEntity;
import com.liyiqing.secondhandmall.memberservice.service.MemberUserCollectSpuService;

import javax.annotation.Resource;


@Service("memberUserCollectSpuService")
public class MemberUserCollectSpuServiceImpl extends ServiceImpl<MemberUserCollectSpuDao, MemberUserCollectSpuEntity> implements MemberUserCollectSpuService {

    @Resource
    private MemberUserCollectSpuDao memberUserCollectSpuDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberUserCollectSpuEntity> page = this.page(
                new Query<MemberUserCollectSpuEntity>().getPage(params),
                new QueryWrapper<MemberUserCollectSpuEntity>()
        );

        return new PageUtils(page);
    }


    //注入商品的远程服务
    @Resource
    private ProductFeignService productFeignService;


    /**
     * 工具：
     * 根据数据库的一条数据，借助远程服务productFeignService，
     * 创建一个展示给前端的MemberCollectSpuVo对象
     */
    @Override
    public MemberCollectSpuVo createMemberCollectSpuVo(MemberUserCollectSpuEntity userCollect){
        //创建 要返回给前端的对象
        MemberCollectSpuVo memberCollectSpuVo = new MemberCollectSpuVo();
        //获得 商品id
        Long spuId = userCollect.getSpuId();

        //调用 远程的商品服务，通过商品id 获取该商品的简要信息
        R r = productFeignService.selectShowProductById(spuId);
        Object o = r.get("data");   //获取data中的数据
        JSONObject json = (JSONObject) JSON.toJSON(o);//将其转为json对象
        //将json对象还原成实体类对象
        ProductShowVo productShowVo = JSONObject.parseObject(String.valueOf(json), ProductShowVo.class);

        //为前端展示对象 拷贝一份重复的属性
        BeanUtils.copyProperties(userCollect,memberCollectSpuVo);

        memberCollectSpuVo.setSpuImg(productShowVo.getImage());//添加图片url
        memberCollectSpuVo.setSpuName(productShowVo.getName());//添加商品名称
        memberCollectSpuVo.setDescription(productShowVo.getDescription());//添加商品描述
        memberCollectSpuVo.setGroupName(productShowVo.getGroupName());//添加商品二级分类名称
        memberCollectSpuVo.setPrice(productShowVo.getPrice());//添加商品单价

        return memberCollectSpuVo;

    }



    /**
     * 根据 买家账号id 查看所有收藏记录
     */
    @Override
    public R selectRecordByMemberId(Long memberId) {
        List<MemberUserCollectSpuEntity> recordList = memberUserCollectSpuDao.selectRecordByMemberId(memberId);
        if (recordList == null || recordList.size()==0){
            return R.error(StatusCode.DATA_NOT_FOUND.getCode(),StatusCode.DATA_NOT_FOUND.getMsg());
        }

        List<MemberCollectSpuVo> memberCollectSpuVoList = new ArrayList<>();
        //根据商品id获取简要的展示信息
        for (MemberUserCollectSpuEntity record:recordList) {
            memberCollectSpuVoList.add(this.createMemberCollectSpuVo(record));
        }

        return R.ok().put("data",memberCollectSpuVoList);
    }



    /**
     * 根据 买家账号id 和 商品id，查看是否存在收藏记录
     */
    @Override
    public MemberUserCollectSpuEntity selectRecordByMidAndSid(Long memberId, Long spuId) {
        return memberUserCollectSpuDao.selectRecordByMidAndSid(memberId,spuId);
    }


}